---
title: "Table 2"
header-includes:
  - \usepackage{caption}
  - \captionsetup[table]{labelformat=empty}
output: pdf_document
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(knitr)
library(survey)
library(foreign)
library(car)
library(xtable)
mil_conf.df <- read.csv("~/Dropbox/Public_Conf_Mil/Data_and_code/mil_conf.csv")
```

```{r echo = FALSE}
source("~/Dropbox/Public_Conf_Mil/Data_and_code/PCM_cleaning.R")

milconf_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.df
  )
```

```{r echo = FALSE}
tab2 <- matrix(NA, nrow = 19, ncol = 3)
rownames(tab2) <- c("18-34", "35-54", "55+", "Male", "Female", "Active Duty Military", "Veteran", "Non-Veteran",
                    "Family in Military", "Social Contact with Military", "White", "Hispanic", "Black",
                    "High School or Less", "College Degree", "Graduate Degree", "Urban", "Suburban",
                    "Rural")
colnames(tab2) <- c("Quite a lot", "A great deal", "Total")
tab2[1,1] <- (svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,4])/2 * 100
tab2[1,2] <- (svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,3])/2 * 100
tab2[1,3] <- tab2[1,1] + tab2[1,2]
tab2[2,1] <- (svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[3,4] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[4,4])/2 * 100
tab2[2,2] <- (svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[3,3] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[4,3])/2 * 100
tab2[2,3] <- tab2[2,1] + tab2[2,2]
tab2[3,1] <- (svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[5,4] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[6,4] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[7,4])/3 * 100
tab2[3,2] <- (svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[5,3] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[6,3] +
                svyby(~Q8_fac, ~AGE7_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[7,3])/3 * 100
tab2[3,3] <- tab2[3,1] + tab2[3,2]
tab2[4,1] <- svyby(~Q8_fac, ~GENDER_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] * 100
tab2[4,2] <- svyby(~Q8_fac, ~GENDER_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] * 100
tab2[4,3] <- tab2[4,1] + tab2[4,2]
tab2[5,1] <- svyby(~Q8_fac, ~GENDER_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,4] * 100
tab2[5,2] <- svyby(~Q8_fac, ~GENDER_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,3] * 100
tab2[5,3] <- tab2[5,1] + tab2[5,2]
tab2[6,1] <- svyby(~Q8_fac, ~VETERAN2_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] * 100
tab2[6,2] <- svyby(~Q8_fac, ~VETERAN2_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] * 100
tab2[6,3] <- tab2[6,1] + tab2[6,2]
tab2[7,1] <- svyby(~Q8_fac, ~VETERAN_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] * 100
tab2[7,2] <- svyby(~Q8_fac, ~VETERAN_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] * 100
tab2[7,3] <- tab2[7,1] + tab2[7,2]
tab2[8,1] <- svyby(~Q8_fac, ~VETERAN_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,4] * 100
tab2[8,2] <- svyby(~Q8_fac, ~VETERAN_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,3] * 100
tab2[8,3] <- tab2[8,1] + tab2[8,2]
tab2[9,1] <- svyby(~Q8_fac, ~Family_2_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,4] * 100
tab2[9,2] <- svyby(~Q8_fac, ~Family_2_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,3] * 100
tab2[9,3] <- tab2[9,1] + tab2[9,2]
tab2[10,1] <- svyby(~Q8_fac, ~Q45_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] * 100
tab2[10,2] <- svyby(~Q8_fac, ~Q45_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] * 100
tab2[10,3] <- tab2[10,1] + tab2[10,2]
tab2[11,1] <- svyby(~Q8_fac, ~RACETHNICITY_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] * 100
tab2[11,2] <- svyby(~Q8_fac, ~RACETHNICITY_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] * 100
tab2[11,3] <- tab2[11,1] + tab2[11,2]
tab2[12,1] <- svyby(~Q8_fac, ~RACETHNICITY_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[4,4] * 100
tab2[12,2] <- svyby(~Q8_fac, ~RACETHNICITY_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[4,3] * 100
tab2[12,3] <- tab2[12,1] + tab2[12,2]
tab2[13,1] <- svyby(~Q8_fac, ~RACETHNICITY_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,4] * 100
tab2[13,2] <- svyby(~Q8_fac, ~RACETHNICITY_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,3] * 100
tab2[13,3] <- tab2[13,1] + tab2[13,2]
tab2[14,1] <- (svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[3,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[4,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[5,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[6,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[7,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[8,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[9,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[10,4])/10 * 100
tab2[14,2] <- (svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[3,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[4,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[5,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[6,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[7,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[8,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[9,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[10,3])/10 * 100
tab2[14,3] <- tab2[14,1] + tab2[14,2]
tab2[15,1] <- (svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[11,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[12,4])/2 * 100
tab2[15,2] <- (svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[11,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[12,3])/2 * 100
tab2[15,3] <- tab2[15,1] + tab2[15,2]
tab2[16,1] <- (svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[13,4] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[14,4])/2 * 100
tab2[16,2] <- (svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[13,3] +
                 svyby(~Q8_fac, ~EDUC_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[14,3])/2 * 100
tab2[16,3] <- tab2[16,1] + tab2[16,2]
tab2[17,1] <- svyby(~Q8_fac, ~URBAN3_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,4] * 100
tab2[17,2] <- svyby(~Q8_fac, ~URBAN3_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[1,3] * 100
tab2[17,3] <- tab2[17,1] + tab2[17,2]
tab2[18,1] <- svyby(~Q8_fac, ~URBAN3_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,4] * 100
tab2[18,2] <- svyby(~Q8_fac, ~URBAN3_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[2,3] * 100
tab2[18,3] <- tab2[18,1] + tab2[18,2]
tab2[19,1] <- svyby(~Q8_fac, ~URBAN3_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[3,4] * 100
tab2[19,2] <- svyby(~Q8_fac, ~URBAN3_fac+DOV_ASSIGNMENT_A, milconf_design, svymean, na.rm = TRUE)[3,3] * 100
tab2[19,3] <- tab2[19,1] + tab2[19,2]
tab2_xt <- xtable(tab2)
digits(tab2_xt) <- 0
align(tab2_xt) <- "rccc"
```

```{r echo = FALSE, results = "asis"}
print(tab2_xt, comment = FALSE, caption.placement = "top", size = "\\large")
```

